<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文章类别列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/oksub.css">
    <script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
    <!--模糊搜索区域-->
    <div class="layui-row">
        <form class="layui-form layui-col-md12 ok-search">
            <input class="layui-input" placeholder="请输入标题" autocomplete="off" name="title">
            <button class="layui-btn" lay-submit="" lay-filter="search">
                <i class="layui-icon layui-icon-search"></i>
            </button>
        </form>
    </div>
    <div class="layui-form-item">
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test2">上传</button>
            <input type="hidden" id="url" name="url">
            <input type="hidden" id="smUrl" name="smUrl">
            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                预览图：
                <div class="layui-upload-list" id="demo2"></div>
            </blockquote>
        </div>
    </div>
    <!--数据表格-->
    <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
    var rowData;
    layui.use(["element", "table", "form", "laydate", "okLayer", "okUtils", "okMock","upload"], function () {
        let table = layui.table;
        let form = layui.form;
        let util = layui.util;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        let okMock = layui.okMock;
        let upload = layui.upload;
        okLoading.close();
        util.fixbar({});

        laydate.render({elem: "#startTime", type: "datetime"});
        laydate.render({elem: "#endTime", type: "datetime"});

        upload.render({
            elem: '#test2'
            ,url: '/api/file/uploadVideo' //改成您自己的上传接口
            ,multiple: true
            ,accept:'video'
            ,before: function(obj){
                loadIndex=layer.load(0, {shade: 0.3});
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){

                });
            }
            ,done: function(res){
                //上传完毕
                layer.close(loadIndex);
                layer.msg(res.msg, {icon: 7, time: 2000});
            }
        });

        let videoTable = table.render({
            elem: "#tableId",
            url: '/articleCategory/list',
            limit: 20,
            page: true,
            even: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: "id", title: "ID", sort: true},
                {field: "name", title: "类别名称"},
                {field: "createTime", title: "创建时间"},
                {field: "updateTime", title: "更新时间"},
                {title: "操作", align: "center", fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count)
            },
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'size' //每页数据量的参数名，默认：limit
            },
            response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
        });

        form.on("submit(search)", function (data) {
            articleTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "batchEnabled":
                    batchEnabled();
                    break;
                case "batchDisabled":
                    batchDisabled();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "add":
                    add();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    rowData=data;
                    edit(data.id);
                    break;
                case "del":
                    del(data.id);
                    break;
            }
        });

        function batchEnabled() {
            okLayer.confirm("确定要批量上架吗？", function (index) {
                layer.close(index);
                let idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("/articleCategory/del", "put", {ids: idsStr}, true).done(function (response) {
                        console.log(response);
                        okUtils.tableSuccessMsg(response.msg);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function batchDisabled() {
            okLayer.confirm("确定要批量下架吗？", function (index) {
                layer.close(index);
                var idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("/article/batchStop", "put", {idsStr: idsStr}, true).done(function (response) {
                        console.log(response);
                        okUtils.tableSuccessMsg(response.msg);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                var idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("/articleCategory/del", "put", {ids: idsStr}, true).done(function (response) {
                        console.log(response);
                        okUtils.tableSuccessMsg(response.message);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function add() {
            okLayer.open("添加文章类别", "articleCategory-add", "50%", "50%", null, function () {
                articleTable.reload();
            })
        }

        function edit(id) {
            okLayer.open("编辑文章类别", "articleCategory-edit", "50%", "50%", null, function () {
                articleTable.reload();
            })
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("/articleCategory/del", "get", {ids: id}, true).done(function (response) {
                    console.log(response);
                    okUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <div class="layui-btn-container">
        <!--<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchEnabled">批量上架</button>-->
        <!--<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchDisabled">批量下架</button>-->
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<!-- 链接模板 -->
<script type="text/html" id="urlTpl">
    <a href="{{d.url}}" target="_blank">{{d.url}}</a>
</script>
<!-- 是否指顶模板
		<input type="checkbox" name="isTop" value="{{d.isTop}}" lay-skin="switch" lay-text="是|否" {{ d.isTop== true ? 'checked' : ''}}>
-->
<script type="text/html" id="topTpl">
    {{#  if(d.isTop == true){ }}
    <span class="layui-btn layui-btn-normal layui-btn-xs">已置顶</span>
    {{#  } else if(d.isTop == false) { }}
    <span class="layui-btn layui-btn-warm layui-btn-xs">未置顶</span>
    {{#  } }}
</script>
<!-- 发布状态模板
		<input type="checkbox" name="top" value="{{d.status}}" lay-skin="switch" lay-text="已发布|未发布" {{ d.status== true ? 'checked' : ''}}>
-->
<script type="text/html" id="statusTpl">
    {{#  if(d.status == true){ }}
    <span class="layui-btn layui-btn-normal layui-btn-xs">已发布</span>
    {{#  } else if(d.status == false) { }}
    <span class="layui-btn layui-btn-warm layui-btn-xs">未发布</span>
    {{#  } }}
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
    <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
